<?php defined('SYSPATH') or die('No direct script access.');

class Model_Category extends ORM
{ 
    protected $_reload_on_wakeup = false; 
    
    public function get_main_categories()
    {
        if ($categories = Cache::instance()->get('main_categories')) {
            return $categories;
        }
        
        $main_cats = array(1810, 1773, 1763, 1741, 1830, 1834,);
        $categories = ORM::factory('category')
                        ->where('id', 'IN', $main_cats)
                        ->find_all()
                        ->as_array();
        
        Cache::instance()->set('main_categories', $categories);
        return $categories;
    }
    
    public function get_categories_tree()
    {
        if ($categories = Cache::instance()->get('categories_tree')) {
            return $categories;
        }
        
        // Максимум 3 уровня вложенноси, не придумываем ничего универсального
        $_first_row = $this->get_top_categories();
        $first_row = array();
        
        $ids = array();
        foreach ($_first_row as $category) {
            $ids[] = $category->id; 
            $first_row[$category->id] = $category->as_array();
            $first_row[$category->id]['active'] = false;
            $first_row[$category->id]['items']  = array();  
        }
        $_second_row = $this->_get_categories_query()
                    ->where('parent_id', 'IN', $ids)
                    ->find_all();
        $second_row = array();
                    
        $ids = array();
        foreach ($_second_row as $category) {
            $ids[] = $category->id; 
            $second_row[$category->id] = $category->as_array();
            $second_row[$category->id]['active'] = false;
            $second_row[$category->id]['items']  = array();
        }
        $third_row = $this->_get_categories_query()
                    ->where('parent_id', 'IN', $ids)
                    ->find_all()
                    ->as_array();
                    
        foreach ($third_row as &$category) {
            $category = $category->as_array();
            $category['active'] = true;
            $second_row[$category['parent_id']]['items'][] = $category;   
        }
        
        foreach ($second_row as &$category) {
            $first_row[$category['parent_id']]['items'][] = $category;    
        }
        
        Cache::instance()->set('categories_tree', $first_row);
        return $first_row;        
    }
    
    public function get_top_categories()
    {
        if ($categories = Cache::instance()->get('top_categories')) {
            return $categories;
        }
        $categories = $this->_get_categories_query()->where('parent_id', '=', 0)->order_by('rank', 'ASC')->find_all(); 
        Cache::instance()->set('top_categories', Model_Main::get_array($categories));
        return $categories;
    } 
    
    public function get_childs()
    {
        if ($categories = Cache::instance()->get('categories_'.$this->id)) {
            return $categories;
        }
        $categories = $this->_get_categories_query()->where('parent_id', '=', $this->id)->find_all();
        Cache::instance()->set('categories_'.$this->id, Model_Main::get_array($categories));
        return $categories;  
    }
    
    protected function _get_categories_query()
    {
        return ORM::factory('category');  
    }
    
    public function get_breadcrumbs()
    {
        // TODO cache
        $breadcrumbs = array();
        $category_id = $this->parent_id;
        $old_category_id = $category_id;
        
        while ($category_id) {
            $category = ORM::factory('category', $category_id);
            $breadcrumbs[] = $category;   
            
            $category_id = $category->parent_id;
        }
        
        $breadcrumbs = array_reverse($breadcrumbs);
        return $breadcrumbs;    
    }
}
